﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WxxMail.MVC.Factories;
using WxxMail.MVC.Models.Login;

namespace WxxMail.MVC.Controllers
{ 
    public class LoginController : Controller
    {
        ILoginFactory _loginFactory = new LoginFactory();
        // GET: Login
        
        public ActionResult Index()
        {
            return View();
          
        }
        /// <summary>
        /// 用户名密码提交
        /// </summary>
        /// <param name="loginModel"></param>
        /// <returns></returns>
        public ActionResult submit(LoginModel _loginModel,String code)
        {
           //用户名密码不为空
            if (ModelState.IsValid)
            {
                //用户名密码正确
                if (_loginFactory.LoginValue(_loginModel))
                {
                    if (Session["ValidateCode"].ToString() == code)
                    {
                        FormsAuthentication.SetAuthCookie(_loginModel.LoginId, false);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("Code", "验证码错误！！");
                        return View("Index");
                    }
                }
                else
                {
                    ModelState.AddModelError("CredentialError", "用户名密码错误！！");
                    return View("Index");
                }
            }
            else
            {
                return View("Index");
            }
            
        }
    }
}